//
//  CustomNavigationCtrl.m
//
//  Created by Northwan on 15-4-12.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "CustomNavigationCtrl.h"
#import "YSAlertView.h"
#import "YSPushModel.h"
#import "YSFeedbackCtrl.h"
#import "YSServiceChatCtrl.h"

@interface CustomNavigationCtrl ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
@end

@implementation CustomNavigationCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
        self.delegate = weakSelf;
    }
}

+(void)initialize{
    //设置导航栏的顶部文字颜色与大小
    UINavigationBar *appearance = [UINavigationBar appearance];
//    [appearance setBarTintColor:[UIColor colorWithWhite:1.0f alpha:0.5f]];
    // 设置文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = CustomNavigationTitleColor;
    textAttrs[NSFontAttributeName] = CustomNavigationTitleFont;
    // UIOffsetZero是结构体, 只要包装成NSValue对象, 才能放进字典\数组中
    [appearance setTitleTextAttributes:textAttrs];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //如果在push过程中触发手势滑动返回，会导致导航栏崩溃（从日志中可以看出）。针对这个问题，我们需要在pop过程禁用手势滑动返回功能
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
        self.interactivePopGestureRecognizer.enabled = NO;
    if (self.viewControllers.count > 0) { // 如果现在push的不是栈底控制器(最先push进来的那个控制器)
        viewController.hidesBottomBarWhenPushed = YES;
        // 设置导航栏按钮
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"nav_return_normal" highImageName:@"nav_return_normal" target:self action:@selector(back)];
    }
    [super pushViewController:viewController animated:animated];
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.interactivePopGestureRecognizer.enabled = YES;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotiDeal:) name:kRemotePushNortif object:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kRemotePushNortif object:nil];
}

-(void)pushNotiDeal:(NSNotification *)notification{
    NSDictionary *userInfo = notification.userInfo;
    [self dealNoti:userInfo isEnterForeground:theApp.isEnterForeground];
}

-(void)dealNoti:(NSDictionary *)userInfo isEnterForeground:(BOOL)isEnterForeground{
    NSLog(@"userInfo-----------:%@",userInfo);
    YSPushModel *pushModel =  [YSPushModel objectWithKeyValues:userInfo];
    if (isEnterForeground) {
        [self dealNortifNextAction:pushModel isEnterForeground:isEnterForeground];
    }else{
        [self showNortifAlertView:pushModel];
    }
}

-(void)showNortifAlertView:(YSPushModel *)pushModel{
    YSAlertView *alert = [[YSAlertView alloc]initWithTitle:pushModel.title message:pushModel.content delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"打开 ", nil];
    alert.obj = pushModel;
    [alert setDelegate:self];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        //打开推送过来的通知
        YSAlertView *ysalertView = (YSAlertView *)alertView;
        YSPushModel *pushModel = ysalertView.obj;
        //处理弹出的alertview确认的跳转
        [self dealNortifNextAction:pushModel isEnterForeground:NO];
    }
}

-(void)dealNortifNextAction:(YSPushModel *)pushModel isEnterForeground:(BOOL)isEnterForeground{
    switch (pushModel.messageType) {
        case PushMessageOpenAppType:
            NSLog(@"推送打开app");
            break;
        case PushMessageVirtualProtocolType:{
            [[NSNotificationCenter defaultCenter]postNotificationName:kToOtherPageNortif object:pushModel.value[0]];
            break;
        }
        case PushMessageDeliverType:
            NSLog(@"发货提醒");
            break;
        case PushMessageWaitPayType:
            NSLog(@"待支付提醒");
            break;
        case PushMessageFeedbackType:{
            NSLog(@"反馈内容回复提醒");
            if([[[self viewControllers] lastObject] isKindOfClass:[YSFeedbackCtrl class]]){
                //直接显示消息
                [[NSNotificationCenter defaultCenter] postNotificationName:kNewChatMessageContentNortif object:pushModel.value];
            }
            break;
        }
        case PushMessageChatType:{
            NSLog(@"在线客服消息提醒");
            if([[[self viewControllers] lastObject] isKindOfClass:[YSServiceChatCtrl class]]){
                //直接显示消息
                [[NSNotificationCenter defaultCenter] postNotificationName:kNewChatMessageContentNortif object:pushModel.value[0]];
            }else{
                //记录有新的消息&显示小红点
                [DefaultsHelper setBool:YES forKey:kHasNewChatMessage];
                [[NSNotificationCenter defaultCenter] postNotificationName:kDealNewMessageBadgeNortif object:nil];
            }
            break;
        }
        default:
            break;
    }
}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    //判断当前是否是第一个控制器，如果是的话则不手势识别
    if(self.viewControllers.count==1){
        return NO;
    }
    return YES;
}

@end
